$(function(){
	
	addtips = $("#add .validateTips");
	
	//给出提示的函数
	function updateaddTips(t) {
		addtips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			addtips.removeClass('ui-state-highlight',1000);
		}, 500);
	};	
	
	//确定增加的对话框
	$("#addConfirm").dialog({
		autoOpen : false,
		/*height :300,*/
		width : 400,
		resizable: false,
		modal : true,
		buttons : {
			'确定' : function()
			{	
			var TName = $.trim($("#addTName").val());
			var TCause = $.trim($("#addTCause").val());
			//alert(TCause);
			var url="/ag/lwc/savetarget.action";
			var req={'target.TName':TName,'target.TCause':TCause};
			var ajax=jQuery.post(url,req,function (data)
				{
				if((typeof data.message) == "undefined")
						data = eval("("+data+")");
				if(data.message=="save_success")
				{
					$("#add").dialog('close');				
					$("#addConfirm").dialog('close');					
					$("#message").text("保存成功");
					$('#message').dialog('open');
					setTimeout("window.location.href='/ag/lwc/findtarget.action';",600); 
				}
				else
				{
					$("#addConfirm").dialog('close');
					$("#add").dialog('close');
					$("#message").text("保存失败");
					$('#message').dialog('open');				
				}
				});

			},
			'取消' : function() 
			{
				$(this).dialog('close');
			}
		},
		close : function()
		{
			$("#addTName").val("");
			updateaddTips("");
			addtips.removeClass('ui-state-highlight');
			$("dialog").dialog('close');
		}
	});
	//增加的对话框,主要用作验证
		$("#add").dialog({
			autoOpen : false,
			/*height :300,*/
			width : 600,
			modal : true,
			buttons : {
				'提交' : function()
				{			
				//获取增加对话框的内容并验证
				var TName = $.trim($("#addTName").val());
				var TCause = $.trim($("#addTCause").val());//alert(TCause);
				var cArr = TName.match(/[^x00-xff]/ig);
				var cArr2 = TCause.match(/[^x00-xff]/ig);
				if((TName.length+(cArr == null ? 0:cArr.length))>100)
				{
					updateaddTips("长度不能超出50个汉字");return false;
				}
				if((TCause.length+(cArr2 == null ? 0:cArr2.length))>200)
				{
					updateaddTips("原因长度不能超出100个汉字");return false;
				}
				else if((TName==null||TName.length==0)&&(TCause==null||TCause.length==0)) {updateaddTips("你没有填入任何东西");return false;}
				
				else $('#addConfirm').dialog('open');
					
		
				},
				'取消' : function() 
				{					
					$(this).dialog('close');
				}
			},
			close : function()
			{
				$("#addTName").val("");
				$("#addTCause").val("");
				updateaddTips("");
				addtips.removeClass('ui-state-highlight');
				$("dialog").dialog('close');
			}
		});


		//点击增加按钮,并打开增加对话框
		$('#addbut').click(function() {
			$("#add").dialog("option","width",600);
			addtips.removeClass('ui-state-highlight');
			$('#add').dialog('open');
		
		});
		
	});